package com.fary.portal.member.controller;

import com.alibaba.fastjson.JSONObject;
import com.fary.base.BaseResponse;
import com.fary.base.BaseWebController;
import com.fary.bean.utils.MeiteBeanUtils;
import com.fary.constants.Constants;
import com.fary.constants.WebConstants;
import com.fary.member.input.dto.UserLoginInputDTO;
import com.fary.portal.member.feign.MemberLoginServiceFeign;
import com.fary.portal.member.vo.LoginVo;
import com.fary.web.utils.CookieUtils;
import com.fary.web.utils.RandomValidateCodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * 登陆请求
 */
@Controller
public class LoginController extends BaseWebController {
	/**
	 * 跳转到登陆页面页面
	 */
	private static final String MB_LOGIN_FTL = "member/login";
	/**
	 * 重定向到首页
	 */
	private static final String REDIRECT_INDEX = "redirect:/";
	@Autowired
	private MemberLoginServiceFeign memberLoginServiceFeign;

	/**
	 * 跳转页面
	 */
	@GetMapping("/login")
	public String getLogin() {
		return MB_LOGIN_FTL;
	}

	/**
	 * 接受请求参数
	 */
	@PostMapping("/login")
	public String postLogin(@ModelAttribute("loginVo") LoginVo loginVo, Model model, HttpServletRequest request,
							HttpServletResponse response, HttpSession httpSession) {
		// 1.图形验证码判断
		String graphicCode = loginVo.getGraphicCode();
		if (!RandomValidateCodeUtil.checkVerify(graphicCode, httpSession)) {
			setErrorMsg(model, "验证码不正确!");
			return MB_LOGIN_FTL;
		}

		// 2.将vo转换为dto
		UserLoginInputDTO userLoginInputDTO = MeiteBeanUtils.voToDto(loginVo, UserLoginInputDTO.class);
		userLoginInputDTO.setLoginType(Constants.MEMBER_LOGIN_TYPE_PC);
		String info = webBrowserInfo(request);
		userLoginInputDTO.setDeviceInfo(info);
		BaseResponse<JSONObject> login = memberLoginServiceFeign.login(userLoginInputDTO);
		if (!isSuccess(login)) {
			setErrorMsg(model, login.getMsg());
			return MB_LOGIN_FTL;
		}
		// 2.将token存入到cookie中  首页读取cookietoken,查询用户信息返回到页面展示
		JSONObject data = login.getData();
		String token = data.getString("token");
		CookieUtils.setCookie(request, response, WebConstants.LOGIN_TOKEN_COOKIENAME, token);
		// 登陆成功后，跳转到首页
		return REDIRECT_INDEX;
	}

	@RequestMapping("/exit")
	public String exit(HttpServletRequest request) {
		// 1. 从cookie中获取token
		String token = CookieUtils.getCookieValue(request, WebConstants.LOGIN_TOKEN_COOKIENAME);
		if (!StringUtils.isEmpty(token)) {
			BaseResponse<JSONObject> delToken = memberLoginServiceFeign.delToken(token);
			if (isSuccess(delToken)) {
				return REDIRECT_INDEX;
			}
		}
		return ERROR_500_FTL;
	}

}
